#include "platformAPI.h"
#include "system_monitor_task.h"
#include "iotgo_service.h"
#include <stdio.h>

#define tskMonitor_PRIORITY (tskIDLE_PRIORITY + 1)

#define LED_ON   0
#define LED_OFF  1

static xTaskHandle systemMonitorTask;

extern int RtlMsleepOS(uint32 ms);

int system_mode;

static void statusLedOn()
{
    GPIO_OUTPUT_SET(GPIO_ID_PIN(13),0);
}

static void statusLedOff()
{
    GPIO_OUTPUT_SET(GPIO_ID_PIN(13),1);
}

static void monitorThread(void *parameter)
{
    int32 monitor_iotgo_state = IOTGO_IDLE;
    int32 monitor_pair_state_count = 0;

    uint8 led_count = 0;

    PIN_FUNC_SELECT(GPIO_PIN_REG(13),FUNC_GPIO13); /* this is for relay*/
    PIN_PULLUP_EN(GPIO_PIN_REG(13));

    statusLedOff();

    while(1)
    {

        if(system_mode == SYS_WORKING_MODE)
        {
            monitor_iotgo_state = iotgoGetState();

            switch(monitor_iotgo_state)
            {
            case IOTGO_IDLE:
                if(led_count == 0)
                {
		    statusLedOn();
                }
                else
                {
		    statusLedOff();
                }
                break;
            case IOTGO_AP_IS_CONNECTED:
                if(led_count == 0 || led_count == 2)
                {
		    statusLedOn();
                }
                else
                {
		    statusLedOff();
                }
                break;
            case IOTGO_DSERVER_IS_CONNECTED:
            case IOTGO_LSERVER_IS_CONNECTED:
                if(led_count < 10)
                {
		    statusLedOn();
                }
                else
                {
		    statusLedOff();
                }
                break;
            case IOTGO_REGISTER_IS_READY:
		statusLedOn();
                break;
            case IOTGO_PAIR_WITH_APP:
                if( (led_count & 0x01) == 0x00 )
                {
		    statusLedOn();
                }
                else
                {
		    statusLedOff();
                }

                monitor_pair_state_count++;
                if(monitor_pair_state_count == 600)
                {
                    iotgoPairWithAppStop();
                    monitor_pair_state_count = 0;
                }
                break;
            case IOTGO_UPGRADE:
                if(led_count == 0 || led_count == 2 || led_count == 4 )
                {
		    statusLedOn();
                }
                else
                {
		    statusLedOff();
                }
                break;
            default:
                break;
            }
        }
        else /*system_mode == FLASH_CODE_MODE or FLASH_DATA_MODE*/
        {
            if(led_count == 0 || led_count == 2 || led_count == 4 )
            {
		statusLedOn();
            }
            else
            {
		statusLedOff();
            }
        }
      
        led_count++;
        if(led_count == 21)
        {
            led_count = 0;
        }
	systemDelayMs(100);
    }
}

int startIotgoSystemMonitorTask(void)
{
    char ret = -1;
    
    if(xTaskCreate(monitorThread, 
		   ((const char*)"monitorThread"), 
		   200, 
		   NULL,
		   tskMonitor_PRIORITY,
		   &systemMonitorTask) != pdPASS)
    {	
        printf("create system monitor thread failed\n");
	return -1;
    }
    return 0;
}

int stopKeySystemMonitorTask(void)
{
    vTaskDelete(systemMonitorTask);
    return 0;
}

